#include "interviewdialog.h"
#include "ui_interviewdialog.h"
#include "debug.h"

InterviewDialog::InterviewDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::InterviewDialog)
{
    ui->setupUi(this);

    init();
}

InterviewDialog::~InterviewDialog()
{
    delete ui;
}

void InterviewDialog::set(const model::InterviewInfo &info)
{
    ui->btn_avatar->setIcon(info.sender.avatar);
    ui->label_username->setText(info.sender.nickname);
    ui->label_time->setText(util::formatTime(info.timestamp));

    _content->setHtml(QString(R"(<h1>%1</h1></pre><div class="blog-content">%2</div>)")
                            .arg(info.title.toHtmlEscaped(), info.content.toHtmlEscaped().replace("\n", "<br/>")));
}

void InterviewDialog::init()
{
    initPanel();
    initBody();
    initStyleSheet();
}

void InterviewDialog::initStyleSheet()
{
    ui->panel->setStyleSheet(R"(background-color: rgb(247, 249, 252); border-radius: 12px; color: #333333;)");
    ui->head->setStyleSheet(R"(
    #head {
        background-color: transparent;
    })");
    ui->btn_close->setStyleSheet(R"(
    QPushButton {
        background-repeat: no-repeat;
        background-position: center;
        background-image: url(:/ui/btn_close_50.png);
        border: none;
        background-color: transparent;
    }

    QPushButton:hover,
    QPushButton:pressed {
        background-image: url(:/ui/btn_close_100.png);
    })");
#if TEST_UI
    ui->btn_avatar->setStyleSheet(R"(background-color: #ff0000;)");
#endif
}

#include <QGraphicsDropShadowEffect>
void InterviewDialog::initPanel()
{
    ui->panel->setFixedSize(1200, 900);

    // 顶层(MainWidget)：无边框 + 透明，仅做容器
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    // 给内容面板(panel)加阴影
    auto *effect = new QGraphicsDropShadowEffect(ui->panel);
    effect->setColor(Qt::black);
    effect->setOffset(0, 0); // 0偏移
    effect->setBlurRadius(6); // 模糊半径
    ui->panel->setGraphicsEffect(effect);

    ui->panel->layout()->setSpacing(0);
    ui->panel->layout()->setContentsMargins(0, 10, 0, 10);

    ui->head->layout()->setSpacing(20);
    ui->head->layout()->setContentsMargins (20, 0, 20, 0);
    ui->head->setFixedHeight(60);
    ui->head_info->layout()->setContentsMargins(10, 0, 0, 0);

    ui->btn_avatar->setFixedSize(50, 50);
    ui->btn_close->setFixedSize(45, 45);
}

#include <QScrollArea>
#include <QTextBrowser>
void InterviewDialog::initBody()
{
    // 1) 新建一个主布局，设置给 ui->body
    QVBoxLayout *mainLayout = new QVBoxLayout(ui->body);
    mainLayout->setContentsMargins(0, 0, 0, 0);

    // 2) 新建 QScrollArea 及其内部容器
    _scrollArea = new QScrollArea(ui->body);

    _container = new QWidget;
    _container->setStyleSheet(R"( background-color: transparent;)");

    QVBoxLayout* containerLayout = new QVBoxLayout(_container);
    containerLayout->setSpacing(10);
    containerLayout->setContentsMargins(20, 0, 20, 0);
    containerLayout->setAlignment(Qt::AlignTop); // 布局内所有子控件顶对齐

    // 3) 设置 scrollArea
    _scrollArea->setWidget(_container);
    _scrollArea->setWidgetResizable(true);
    _scrollArea->setStyleSheet(R"( QScrollArea { border: none; background-color: transparent; } QScrollArea > QWidget { background-color: transparent; } QScrollBar:vertical { background-color: transparent; /* 透明背景 */ width: 9px; border: none; } QScrollBar::handle:vertical { background-color: #dcdfe4; border-radius: 4px; min-height: 20px; /* 最小高度 */ } QScrollBar::handle:vertical:hover { background-color: #bec3ca; } QScrollBar::handle:vertical:pressed { background-color: #a8adb5; /* 新增:按下状态 */ } /* 关键:定义滑块上下的空白区域 */ QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical { background: transparent; } QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical { height: 0px; /* 隐藏箭头 */ background: transparent; } QScrollBar::up-arrow:vertical, QScrollBar::down-arrow:vertical { background: none; } )");

    // 4) 将 scrollArea 加到主布局里
    mainLayout->addWidget(_scrollArea);

    _content = new QTextBrowser(_container);
    _content->setOpenExternalLinks(true);
    _content->setFrameShape(QFrame::NoFrame);
    _content->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    _content->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    _content->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    _content->setStyleSheet(
       "QTextBrowser{ background:transparent; line-height:160%; }"
       "h1,h2,h3{ font-weight:700; margin:12px 0 8px; }"
       "p{ margin:8px 0; }"
       "pre{ background:#f0f0f0; padding:8px; border-radius:6px; }"
       "a{ color:#1e80ff; }"
    );
    QFont font_content("微软雅黑", 10);
    _content->setFont(font_content);

    _container->layout()->addWidget(_content);
}

#include <QMouseEvent>
void InterviewDialog::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        _dragPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
        _isDraging = true;
        event->accept();
    }
}

void InterviewDialog::mouseMoveEvent(QMouseEvent *event)
{
    if ((event->buttons() & Qt::LeftButton) && _isDraging) {
        move(event->globalPosition().toPoint() - _dragPosition);
        event->accept();
    }
}

void InterviewDialog::mouseReleaseEvent(QMouseEvent *event)
{
    (void)event;
    _isDraging = false;
}

void InterviewDialog::on_btn_close_clicked()
{
    InterviewDialog::reject();
}

